package com.cwt_taxi_xa.timer;

import android.util.Log;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.MyStatus;
import com.cwt_taxi_xa.protocol.DispatchCenterClient;
import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.ExceptionUtil;

import java.util.TimerTask;

public class TimerConnecter extends TimerTask {
	private static final String TAG = "TimerConnecter";
	@Override
	public void run() {

		try {
			boolean b = DispatchCenterUsers.checkConnParam();
			if(b){
				Log.e("连接标识", ""+MyStatus.center);
				if (!MyStatus.center) {
					DispatchCenterUsers.times +=1;
					if(MyStatus.online){
						//默认连接10次，如果10次连接不上将不再进行连接
						//50000次间隔10s,也就是
						if(DispatchCenterUsers.times < 50000){
							AppLog.i("【通讯平台】通讯平台开始连接.....................................");
							DispatchCenterClient.connect(1);
						}else{
							if(DispatchCenterUsers.timerConnecter!=null){
								DispatchCenterUsers.timerConnecter.cancel();
							}
						}
					}else{
						AppLog.e(TAG,"网络未连接,放弃连接服务器");
					}
				}else{
					if(DispatchCenterUsers.timerConnecter!=null){
						DispatchCenterUsers.timerConnecter.cancel();
					}
				}
			}else{
				// 向主界面更新连接状态
				MainApplication.topActivity.setStatus(2, false);
				MyStatus.center = false;
			}
		} catch (Exception e) {
			AppLog.e(ExceptionUtil.getInfo(e));
			e.printStackTrace();
		}

	}
}
